﻿#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include "cJSON.h"

#define REMOTE_IP  "192.168.0.166"
#define REMOTE_PORT 9877
//打卡结构体
typedef struct pass_card
{
	char type[20];
	char name[20];
	char passcard[20]; 
}P_C;
/*
函数功能：打包json包
函数参数：P_C *pass-打卡结构体
函数返回值：void
*/
char * PassPack(P_C *pass);
/*
函数功能：打卡
函数参数：P_C pass-打卡结构体，sc-服务器套接字
函数返回值：void
*/
void PassCard(P_C pass,int sc,struct tm *local_time);
/*
函数功能：查询所以打卡信息
函数参数：P_C *pass-打卡结构体，sc-服务器套接字
函数返回值：void
*/
int CheckPassAll(P_C pass,int sc);
/*
函数功能：按姓名查询每月打卡
函数参数：P_C *pass-打卡结构体，sc-服务器套接字
函数返回值：void
*/
int CheckPassName(P_C pass,int sc);
/*
函数功能：按时间查询每日打卡
函数参数：P_C *pass-打卡结构体，sc-服务器套接字
函数返回值：void
*/
int CheckPassTime(P_C pass,int sc);
char p[1024] = {0};

int main(int argc,const char* argv[])
{
	int sc = socket(AF_INET,SOCK_STREAM,0);

	if(sc == -1)
	{
		perror("socket");
		return -1;
	}

	char buf[1024] = {0};
	struct sockaddr_in raddr;
	raddr.sin_family = AF_INET;
	raddr.sin_port = htons(REMOTE_PORT);
	raddr.sin_addr.s_addr = inet_addr(REMOTE_IP);
	int iscon = connect(sc,(struct sockaddr*)&raddr,sizeof(raddr));
	if (iscon == -1)
	{
		perror("connect");
		exit(1);
	}
	P_C pass;
	time_t now;
	time(&now);
	struct tm * local_time = localtime(&now);
	int c = 0;
	while(1)
	{
		printf("1--打卡\n");
		printf("2--查询所以打卡信息\n");
		printf("3--按姓名查询每月打卡\n");
		printf("4--按时间查询每日打卡\n");
		printf("-1--退出\n");
		printf("请输入选择\n");
		scanf("%d",&c);
		if (c == -1)
		{
			break;
		}
		switch(c)
		{
			case 1:
			//打卡
				PassCard(pass,sc,local_time);
				break;
			case 2:
			//查询所以打卡信息
				CheckPassAll(pass,sc);
				break;
			case 3:
				//按姓名查询每月打卡
				CheckPassName(pass,sc);
				break;
			case 4:
				//按时间查询每日打卡
				CheckPassTime(pass,sc);
				break;
		}
	}
	close(sc);
    return 0;
}
//打卡打包
char * PassPack(P_C *pass)
{
	//创建一个json对象
	cJSON* root = cJSON_CreateObject();
	//创建一个各数据类型的json对象
	cJSON* type = cJSON_CreateString(pass->type);
	cJSON* name = cJSON_CreateString(pass->name);
	cJSON* passcard = cJSON_CreateString(pass->passcard);
	//向json对象中添加“键-值”对
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"name",name);
	cJSON_AddItemToObject(root,"passcard",passcard);
	//将打包好的json对象copy到p字符串中
	strcpy(p,cJSON_Print(root));

	//释放json对象
	cJSON_Delete(root);
}
//打卡函数
void PassCard(P_C pass,int sc,struct tm *local_time)
{
	memset(&pass,0,sizeof(P_C));
	strcpy(pass.type,"passcard");

	printf("input name:");
	scanf("%s",pass.name);

	printf("input passcard:");
	sprintf(pass.passcard,"%d-%02d-%02d %02d:%02d:%02d",local_time->tm_year + 1900,\
		local_time->tm_mon + 1,
		local_time->tm_mday,
		local_time->tm_hour,
		local_time->tm_min,
		local_time->tm_sec);
		PassPack(&pass);
		//json数据包写入sock；
		write(sc,p,strlen(p));
}
//查询所以打卡信息
int CheckPassAll(P_C pass,int sc)
{
	memset(&pass,0,sizeof(P_C));
	strcpy(pass.type,"checkpassall");
	PassPack(&pass);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}
//按姓名查询每月打卡
int CheckPassName(P_C pass,int sc)
{
	memset(&pass,0,sizeof(P_C));
	strcpy(pass.type,"checkpassname");
	printf("input name:");
	scanf("%s",pass.name);
	PassPack(&pass);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}
//按时间查询每日打卡
int CheckPassTime(P_C pass,int sc)
{
	memset(&pass,0,sizeof(P_C));
	strcpy(pass.type,"checkpasstime");
	printf("input day:");
	scanf("%s",pass.passcard);
	PassPack(&pass);//打包
	write(sc,p,strlen(p));
	while(1)
	{
		//清空p中之前的套接字数据
		memset(p,0,sizeof(p));
		int re = read(sc,p,1024);
		if (strncmp(p,"quit",4)==0)
		{
			break;
		}
		if (strncmp(p,"nothing",7)==0)
		{
			printf("查无信息\n");
			break;
		}
		printf("%s",p);
		//给服务器发送接受成功信息isok
		write(sc,"isok",4);
	}
}